﻿using SuperSocket.SocketBase;
using SuperSocket.SocketEngine;
using System;
using System.Data;
using System.Windows.Forms;
using TransferServerLib;

namespace TestWinFormServer
{
    public partial class Form1 : Form
    {
        bool ServiceStatus = false;
        IBootstrap bootstrap;
        DataTable dt;
        public Form1()
        {
            InitializeComponent();
            dt = new DataTable();
            dt.Columns.Add("SessionId");
            dt.Columns.Add("RemoteEndPoint");
            dt.Columns.Add("IsUpLoading");
            dt.Columns.Add("FileName");
            dt.Columns.Add("FileSize");
            dt.Columns.Add("SaveName");
            dt.Columns.Add("TransferPos");
            dt.Columns.Add("TransferLength");
            dt.Columns.Add("TransferedLength");
            this.dataGridView1.DataSource = dt;
        }

        private void btnStartStop_Click(object sender, EventArgs e)
        {
            if (btnStartStop.Text == "启动服务")
            {
                bootstrap = BootstrapFactory.CreateBootstrap();
                if (!bootstrap.Initialize())
                {
                    ServiceStatus = false;
                    MessageBox.Show("Failed to initialize!");
                    return;
                }
                else
                {
                    StartResult result = bootstrap.Start();
                    if (result == StartResult.Failed)
                    {
                        ServiceStatus = false;
                    }
                    else if (result == StartResult.Success)
                    {
                        var transferServer = bootstrap.GetServerByName("TransferServer") as TransferServer;
                        transferServer.NewSessionConnected += transferServer_NewSessionConnected;
                        ServiceStatus = true;
                    }
                    else
                    {
                        //MessageBox.Show(string.Format("Start result: {0}!", result));
                    }
                    MessageBox.Show(string.Format("Start result: {0}!", result));
                }
            }
            else
            {
                bootstrap.Stop();
                ServiceStatus = false;
                MessageBox.Show("The server was stopped!");
            }
            if (ServiceStatus)
                btnStartStop.Text = "停止服务";
            else
                btnStartStop.Text = "启动服务";
        }

        void transferServer_NewSessionConnected(TransferSession session)
        {
            DataRow dr = dt.NewRow();
            dr["SessionId"] = session.SessionID;
            dr["RemoteEndPoint"] = session.RemoteEndPoint;
            dr["IsUpLoading"] = false;
            dr["FileName"] = string.Empty;
            dr["FileSize"] = 0;
            dr["SaveName"] = string.Empty;
            dr["TransferPos"] = 0;
            dr["TransferLength"] = 0;
            dr["TransferedLength"] = 0;
            dt.Rows.Add(dr);
            dt.AcceptChanges();
            session.UpLoadEngine.StartTransfer += (e) =>
            {
                dr["IsUpLoading"] = true;
                dr["FileName"] = e.UpLoadInfo.FileName;
                dr["FileSize"] = e.UpLoadInfo.FileSize;
                dr["TransferPos"] = e.UpLoadInfo.TransferLength;
                dr["TransferLength"] = e.UpLoadInfo.TransferLength;
                dr.AcceptChanges();
                Application.DoEvents();
            };
            session.UpLoadEngine.TransferStep += (e) =>
            {
                dr["TransferedLength"] = session.UpLoadEngine.TransferedLength;
                dr.AcceptChanges();
                Application.DoEvents();
            };
            session.UpLoadEngine.TransferComplete += (e) =>
            {
                dr["IsUpLoading"] = false;
                dr.AcceptChanges();
                Application.DoEvents();
            };
        }
    }
}
